package com.miaosha.ms_05.service;

import com.miaosha.ms_05.dao.OrderDao;
import com.miaosha.ms_05.domain.OrderInfo;
import com.miaosha.ms_05.exception.GlobalException;
import com.miaosha.ms_05.prefix.MiaoshaOrderKey;
import com.miaosha.ms_05.prefix.OrderKey;
import com.miaosha.ms_05.redis.JedisService;
import com.miaosha.ms_05.result.CodeMsg;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description //TODO
 * @Author Administrator
 * @DATE 2020/9/19 16:02
 **/
@Service
public class OrderService {

    @Resource
    private OrderDao orderDao;

    @Resource
    private JedisService jedisService;

    public OrderInfo insert(OrderInfo orderInfo) {
        if (orderInfo == null) {
            throw new GlobalException(CodeMsg.fillParamEmptyArgs("订单信息"));
        }
         orderDao.insert(orderInfo);
        return orderInfo;
    }

    public OrderInfo getOrderInfoById(Long orderId) {
        if (orderId == null) {
            throw new GlobalException(CodeMsg.ORDER_ID_EMPTY);
        }
        return orderDao.getOrderInfoById(orderId);
    }

    public Boolean repeatOrderByUserIdAndGoodsId(Long userId, Long goodsId) {
        if (userId == null) {
            throw new GlobalException(CodeMsg.USER_EMPTY);
        }
        if (goodsId == null) {
            throw new GlobalException(CodeMsg.GOOD_ID_EMPTY);
        }
        return orderDao.countByUserIdAndGoodsId(userId, goodsId) < 1;
    }

    public void delete() {
        orderDao.deleteOrder();
    }


      public void putToRedis(OrderInfo orderInfo){
         jedisService.set(OrderKey.ORDER_KEY,orderInfo.getId().toString(),orderInfo,OrderInfo.class);
      }
}
